﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeAngel.Domain
{
    public static class Extensions
    {
        #region IsSomething
        
        /// <summary>
        /// Identifies if an object is not null.
        /// </summary>
        public static bool IsSomething(this object thisObject)
        {
            return thisObject != null;
        }

        #endregion

        #region IsNotNullOrEmpty

        /// <summary>
        /// Identifies if a string is not null and has at least one non-whitespace character.
        /// </summary>
        public static bool IsNotNullOrEmpty(this string thisString)
        {
            return thisString.IsSomething() && 
                   thisString.Trim().Length > 0;
        }

        #endregion

        #region PropertyDefs
        
        /// <summary>
        /// Gets just the Entity PropertyDefs from a collection of PropertyDefs, sorted by name.
        /// </summary>
        public static IEnumerable<PropertyDef> EntityProperties(this IEnumerable<PropertyDef> PropertyDefs)
        {
            return PropertyDefs.Where(ThisPD => ThisPD.IsEntityType).ToList().SortByName();
        }

        /// <summary>
        /// Gets a sorted collection of PropertyDef objects by name from the a collection.
        /// </summary>
        public static IEnumerable<PropertyDef> SortByName(this IEnumerable<PropertyDef> PropertyDefs)
        {
            return PropertyDefs.OrderBy(ThisPD => ThisPD.Name).ToList();
        }

        #endregion

        #region ToPlural

        /// <summary>
        /// Returns the English plural form of the identified string.
        /// </summary>
        public static string ToPlural(this string thisString)
        {
            if (thisString.EndsWith("s"))
            {
                return thisString + "es";
            }
            else if (thisString.EndsWith("y"))
            {
                //return thisString.Replace("y", "ies");
                return thisString.Substring(0, thisString.Length - 2) + "ies";
            }
            else
            {
                return thisString + "s";
            }
        }

        #endregion

        //public static IEnumerable<DataElementDto> GetChildDataElementDtos(this IEnumerable<DataElementDto> dataElementDtos, Guid parentDataElementID)
        //{
        //    //return dataElementDtos.fin

        //    var ChildDataElementDtos = new List<DataElementDto>();

        //    foreach (var ThisDataElementDto in dataElementDtos)
        //    {

        //    }

        //    return ChildDataElementDtos;
        //}

    }
}
